package dfs_bfs.test394;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/10 11:05
 */
public class Solution2 {

    public String decodeString(String s) {
        String res = "";
        for(int i = 0; i < s.length(); ){
            if(s.charAt(i) < '0' || s.charAt(i) > '9'){
                res += s.charAt(i++);
            }else{
                int k = 0;
                while(s.charAt(i) <= '9' && s.charAt(i) >= '0'){
                    k = k * 10 + s.charAt(i++) - '0';
                }
                int j = i + 1, count = 1;
                while(count > 0){
                    if(s.charAt(j) == '[') count++;
                    if(s.charAt(j) == ']') count--;
                    j++;
                }
                String temp = decodeString(s.substring(i + 1, j - 1));
                while(k-- > 0){
                    res += temp;
                }
                i = j;
            }
        }
        return res;
    }
}
